<template>
  <div>
        <!-- 头部区域 -->
    <div class="rows">
      <el-card  class="ro">
<el-row>
  <el-col :span="24"><div>
    <el-form :inline="true" :model="from" ref="mopa">
  <el-form-item  label="合作商搜索" prop="name">
    <el-input v-model="pars.name"  placeholder="请输入" @input="inp">
    <i slot="suffix" class="el-icon-circle-close" v-show="show" @click="shows" ></i>
    </el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="chaxun">查询</el-button>
  </el-form-item>
</el-form>
    </div>
    </el-col>
</el-row>
      </el-card>
    </div>
                      <!-- 表格区域 -->
                <div class="op">
<el-card class="box-card" >
  <el-row>
  <el-col :span="24"><div>
    <el-button icon="el-icon-circle-plus" type="danger" @click="xinj">新建</el-button>
    </div>
    </el-col>
              <!-- 新建合作商弹窗区域 -->
    <el-dialog  title="新增合作商" width="40%" :visible="dialogVisi" @close="dialogVis">
             <span slot="footer">
    <el-form  label-width="100px" :model="HzShang" :rules="rules" ref="hzsang">
             <el-form-item label="合作商名称" prop="name" >
                <el-input v-model="HzShang.name" maxlength="10" show-word-limit ></el-input>
                </el-form-item>
       <el-form-item label="联系人" prop="contact">
    <el-input v-model="HzShang.contact" type="text" maxlength="10" show-word-limit></el-input>
              </el-form-item>
              <el-form-item label="联系电话" prop="mobile">
    <el-input v-model="HzShang.mobile" type="tel" maxlength="11" show-word-limit></el-input>
              </el-form-item>
           <el-form-item label="分成比列" prop="ratio" >
         <el-input-number v-model="HzShang.ratio" controls-position="right" style="width:95%;heigth:10%;" ></el-input-number>
              </el-form-item>
              <el-form-item label="账号" prop="account">
            <el-input v-model="HzShang.account" type="" maxlength="18" show-word-limit></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
            <el-input v-model="HzShang.password" type="password" maxlength="20" show-word-limit></el-input>
              </el-form-item>
         </el-form>
    <el-button @click="dialogVis">取消</el-button>
    <el-button type="primary" @click="sumbit">确 定</el-button>
  </span>
</el-dialog>
           <!-- 新建合作商弹窗尾部 -->
    <el-col>
                  <!-- 表格内容区域 -->
       <el-table style="width: 100%" :data="Pars">
      <el-table-column  type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="name" label="合作商名称" width="150"></el-table-column>
      <el-table-column prop="vmCount" label="账号" width="150"></el-table-column>
      <el-table-column prop="vmCount" label="设备数量" width="150"></el-table-column>
      <el-table-column prop="ratio" label="分成比列" width="180" />
      <el-table-column prop="contact" label="联系人" width="160" />
      <el-table-column prop="mobile" label="联系电话" width="160" />
      <el-table-column  label="操作" width="220">
            <template  #default="{ row }">
          <div class="flx">
        <el-button type="text" size="small" @click="chongzhi(row.id)">重置密码</el-button>
        <el-button type="text" size="small" @click="chakan(row)">查看详情</el-button>
        <el-button type="text" size="small" @click="xiugai(row.id)">修改</el-button>
        <el-button type="danger" size="small" @click="shanchu(row.id)">删除</el-button>
          </div>
      </template>
      </el-table-column>
    </el-table>
                    <!-- 表格内容尾部 -->
    </el-col>
</el-row>
<!-- 分页 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
           <el-button @click="shang">上一页</el-button>
           <el-button @click="xia">下一页</el-button>
        </el-row>
</el-card>
          <!-- 查看详情弹窗区域 -->
           <el-dialog title="合作商详情" width="40%" :visible="visiblecha" @close="visiblechas">
          <el-row>
 <el-col :span="24"><div>
        <el-descriptions>
          <el-descriptions-item label="合作商名称">{{name}}</el-descriptions-item>
          <el-descriptions-item label="联系人">{{contact}}</el-descriptions-item>
        </el-descriptions>
  </div></el-col>
  <el-col :span="24">
 <el-descriptions>
          <el-descriptions-item label="联系电话">{{mobile}}</el-descriptions-item>
          <el-descriptions-item label="分成比列">{{ratio}}</el-descriptions-item>
        </el-descriptions>
  </el-col>
          </el-row>
           </el-dialog>
           <!-- 修改合作商弹窗区域 -->
 <el-dialog  title="修改合作商" width="40%" :visible="xiuvb" @close="diavxiu">
             <span slot="footer">
    <el-form  label-width="100px" :model="HzShang" :rules="rules" ref="hzsang">
             <el-form-item label="合作商名称" prop="name" >
                <el-input v-model="HzShang.name" maxlength="10" show-word-limit ></el-input>
                </el-form-item>
       <el-form-item label="联系人" prop="contact">
    <el-input v-model="HzShang.contact" type="text" maxlength="10" show-word-limit></el-input>
              </el-form-item>
              <el-form-item label="联系电话" prop="mobile">
    <el-input v-model="HzShang.mobile" type="tel" maxlength="11" show-word-limit></el-input>
              </el-form-item>
           <el-form-item label="分成比列" prop="ratio" >
         <el-input-number v-model="HzShang.ratio" controls-position="right" style="width:95%;heigth:10%;" :min="0" :max="10"></el-input-number>
              </el-form-item>
         </el-form>
    <el-button @click="diavxiu">取消</el-button>
    <el-button type="primary" @click="sumbit">确 定</el-button>
  </span>
</el-dialog>
            <!-- 修改合作商弹窗尾部 -->
                </div>
  </div>
</template>

<script>
import { ParTner, partners, PartenXiu, GenGai, ResetPwd, Dealpar } from '@/api'
export default {
  data () {
    return {
      show: false,
      Pars: [], // 表格数据
      dialogVisi: false,
      xiuvb: false,
      visiblecha: false,
      from: {
        name: ''
      },
      name: '', // 合作商名称
      contact: '', // 联系人
      mobile: '', // 手机号
      ratio: '', // 比列
      pars: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      HzShang: {
        name: '', // 合作商名称
        account: '', // 账号
        password: '', // 密码
        contact: '', // 联系人
        ratio: '', // 分成比列
        mobile: ''// 手机号
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        ratio: [
          { required: true, message: '请输入比列', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请填写手机号码', trigger: 'blur' },
          { min: 11, max: 11, message: '手机号码不低于11位数', trigger: 'blur' }
          // { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '请输入正确的手机号码格式' }
        ]
      }
    }
  },
  created () {
    this.par()
  },
  methods: {
    shows () {

    },
    inp () {
      this.show = true
    },
    // 查询
    chaxun () {
      this.par()
    },
    // 表格数据
    async  par () {
      const re = await ParTner(this.pars)
      this.Pars = re.data.currentPageRecords
    },
    // 新建弹窗
    xinj () {
      this.dialogVisi = true
    },
    dialogVis () {
      this.dialogVisi = false
      this.$refs.hzsang.resetFields()
    },
    // 确定新建
    sumbit () {
      this.$refs.hzsang.validate(async (valid) => {
        if (!valid) return
        if (this.HzShang.id) {
          await GenGai(this.HzShang)
        } else {
          await partners(this.HzShang)
        }
        this.$message.success('新增角色成功')
        this.dialogVis()
        this.diavxiu()
        this.par()
      })
    },
    // 修改
    async xiugai (id) {
      this.xiuvb = true
      const re = await PartenXiu(id)
      this.HzShang = re.data
    },
    diavxiu () {
      this.xiuvb = false
    },
    // 查看详情
    async  chakan (row) {
      this.visiblecha = true
      await ParTner(this.pars)
      this.name = row.name
      this.contact = row.contact
      this.mobile = row.mobile
      this.ratio = row.ratio
    },
    visiblechas () {
      this.visiblecha = false
    },
    // 重置密码
    chongzhi (id) {
      this.$confirm('确定要重置合作商密码吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await ResetPwd(id)
        this.$message.success('重置密码成功')
        this.par()
      })
    },
    // 删除
    shanchu (id) {
      // 1. 询问用户
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 2. 调用删除接口
        await Dealpar(id)
        // 3. 提示用户
        this.$message.success('删除角色成功')
        // 最后一页最后一条数据时，页数减 1
        if (this.Pars.length === 1 && this.pars.pageIndex > 1) {
          this.pars.pageIndex--
        }
        // 4. 刷新列表
        this.par()
      })
    },
    // 分页
    shang () {
      if (this.pars.pageIndex === 1) {
        return false
      } else {
        this.pars.pageIndex--
        this.par()
      }
    },
    xia () {
      this.pars.pageIndex++
      this.par()
    }
  }
}
</script>

<style scoped lang="less">
.rows{
  padding: 10px;
  .ro{
    padding: 5px;
  }
}
.op{
  padding: 10px;
  .box-card{
  padding: 5px;
  margin-top: 10px;
}
}
.flx{
  display: flex;
}
</style>
